<?php
class History extends Controller {
	
	var $name = 'history';
	
	public function index_action() {
		
		$this->bar_title = 'Histórico :: Unidades';
		
		$userid = $this->USER->id;
		$allUnits = $this->getUnitsMaxNote($userid);
		$data['allUnits'] = $allUnits;
		
		$this->view('index', $data);
	}
	
	public function exams() {
		
		$this->bar_title = 'Histórico :: Exames';
		
		$userid = $this->USER->id;
		$unitid = $this->getParam('unit');

		$allExams = $this->getExams($userid, $unitid);
		$data['allExams'] = $allExams;
		$data['unitid'] = $unitid;
		
		$this->view('exams', $data);
	}
	
	public function questions() {
		$this->bar_title = 'Hisórico :: Questões';
		
		$examid = $this->getParam('exam');
		$unitid = $this->getParam('unit');	
					
		$allQuestions = $this->getQuestions($examid);
		$data['allQuestions'] = $allQuestions;
		$note = $this->getNote($examid);
		$data['note'] = $note;
		$data['unitid'] = $unitid;
		
		$this->view('questions', $data);		
	}
	
	/**
	 * 
	 * Retorna todos os exame com nota máxima de cada unidade que o estudante fez
	 * @param unknown_type $userid
	 */
	private function getExamsStudentMaxNote($userid) {
		//Vai armazenar o array de exames
		$exams = array();
		
		$sql = "SELECT DISTINCT unitid, userid, id, unitid, MAX(note) as note, date FROM `oaeditor_exams` WHERE `userid`=?";
		$params = array('userid'=>$userid);
		
		$allExams = $this->DB->get_records_sql($sql, $params);
		
		foreach($allExams as $key=>$value) {
			$exams[] = $value;
		}
		
		return $exams;
	}
	
	/**
	 * 
	 * Retorna as unidades que o aluno faz parte
	 * @param unknown_type $unitid
	 */
	private function getUnitsMaxNote($userid) {
		//vai armazenas o array de unidades junto com a nota
		$allUnits = array();
		$exams = $this->getExamsStudentMaxNote($userid);
		
		foreach($exams as $key=>$value) {
			//Recebe a unidade
			$unit = $this->DB->get_records('oaeditor_units', array('id'=>$value->unitid));
			$units['unit'] = $unit[$value->unitid];
			$units['maxNote'] = $value->note;
			//Armazena no array a unidade e a nota máxima atingida pelo aluno
			$allUnits[] = $units; 
		}
		
		return $allUnits;
	}

	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $userid
	 * @param unknown_type $unitid
	 */
	private function getExams($userid, $unitid) {		
		
		$conditions = array('userid'=>$userid, 'unitid'=>$unitid);
		$allExams = $this->DB->get_records('oaeditor_exams', $conditions);
		
		return $allExams;
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $examid
	 */
	private function getQuestions($examid) {
		//
		$questions = array();
		//
		$conditionExam = array('examid'=>$examid);
		$allQuestions = $this->DB->get_records('oaeditor_questions_resolved', $conditionExam);
		//
		foreach($allQuestions as $value) {
			$conditionQuestion = array('id'=>$value->questionid);
			$question = $this->DB->get_record('oaeditor_questions', $conditionQuestion);
			$question->marked = $value->marked;			
			$questions[] = $question;
		}
		
		return $questions;
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $examid
	 */
	private function getNote($examid) {
		//
		$condition = array('id'=>$examid);
		$exam = $this->DB->get_record('oaeditor_exams', $condition);
		
		return $exam->note;		
	}
	
}
